package com.android.launcher3.setting;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.android.launcher3.Launcher;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;

public class ControlCenter extends FrameLayout {
    private final static double SCALE = 1.8;

    private LinearLayout layoutContainer;
    private final View[] vSelectViews = new View[3];

    public ControlCenter(@NonNull Context context) {
        super(context);
        init();
    }

    public ControlCenter(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ControlCenter(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    void init() {
        LayoutInflater.from(getContext()).inflate(Utilities.isLongStripScreen(getContext()) ? R.layout.control_center_strip : R.layout.control_center, this, true);

        layoutContainer = findViewById(R.id.layout_container);
        findViewById(R.id.layout_pull_up).setOnClickListener(v -> Launcher.getLauncher(getContext()).closeControlCenter());
        findViewById(R.id.layout_setting).setOnClickListener(v -> {
            Launcher launcher = Launcher.getLauncher(getContext());
//            launcher.startActivityForResult(new Intent(launcher, LauncherSettingActivity.class), 999);
//            launcher.startActivityForResult(new Intent(launcher, LauncherSettingActivity.class), 999);
            ComponentName component = new ComponentName("com.applepie.setting", "com.applepie.setting.MainActivity");
//            com.applepie.setting/com.applepie.setting.MainActivity
            Intent intent = new Intent();
            intent.setComponent(component);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            launcher.startActivityForResult(intent, 999);
            launcher.closeControlCenter();
        });

        findViewById(R.id.layout_wifi).setOnClickListener(v -> {
            Launcher launcher = Launcher.getLauncher(getContext());
            Log.d("TinkerLauncher3", "onWifiClick");
            Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
            launcher.startActivity(intent);
        });

        findViewById(R.id.layout_telephony).setOnClickListener(v -> {

        });

        findViewById(R.id.layout_bluetooth).setOnClickListener(v -> {
            Launcher launcher = Launcher.getLauncher(getContext());
            String pkg = "com.applepie.bluetooth";
            String activityName = "com.applepie.bluetoothcall.MainActivity";
            ComponentName component = new ComponentName(pkg, activityName);
            Intent intent = new Intent();
            intent.setComponent(component);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            launcher.startActivity(intent);
        });

        findViewById(R.id.layout_wallpaper).setOnClickListener(v -> {
            Launcher launcher = Launcher.getLauncher(getContext());
//            Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
//            launcher.startActivity(intent);
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setPackage("com.android.settings");
            intent.setClassName("com.android.settings", "com.android.settings.Settings$WallpaperSettingsActivity");
            launcher.startActivity(intent);
        });


        vSelectViews[0] = findViewById(R.id.v_select1);
        vSelectViews[1] = findViewById(R.id.v_select2);
        vSelectViews[2] = findViewById(R.id.v_select3);

        findViewById(R.id.layout_select1).setOnClickListener(v -> {
            ControlCenterManager.INSTANCE.saveIndex(getContext(), 0);
            setUpSelect();
            ///修改样式后，重新设置view
            Launcher.getLauncher(getContext()).getWorkspace().setUpFirstScreen();

        });

        findViewById(R.id.layout_select2).setOnClickListener(v -> {
            ControlCenterManager.INSTANCE.saveIndex(getContext(), 1);
            setUpSelect();
            ///修改样式后，重新设置view
            Launcher.getLauncher(getContext()).getWorkspace().setUpFirstScreen();

        });

        findViewById(R.id.layout_select3).setOnClickListener(v -> {
            ControlCenterManager.INSTANCE.saveIndex(getContext(), 2);
            setUpSelect();
            ///修改样式后，重新设置view
            Launcher.getLauncher(getContext()).getWorkspace().setUpFirstScreen();

        });

        setUpSelect();

        screenAdaptation();
    }

    private void setUpSelect() {
        int selectIndex = ControlCenterManager.INSTANCE.getIndex(getContext());
        for (int i = 0; i < vSelectViews.length; i++) {
            vSelectViews[i].setVisibility(i == selectIndex ? VISIBLE : GONE);
        }
    }

    private void screenAdaptation() {

        post(() -> {

            int width = layoutContainer.getWidth();
            int height = layoutContainer.getHeight();

            double scale = (width * 1.0) / (height * 1.0);

            if (scale > SCALE) {
                width = (int) (height * SCALE);
            } else {
                height = (int) ((width * 1.0) / SCALE);
            }

            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) layoutContainer.getLayoutParams();
            layoutParams.width = width;
            layoutParams.height = height;
            layoutContainer.setLayoutParams(layoutParams);
        });
    }

}
